// app/pages/index/item_function/activity/activityindex/activityindex.js
const app = getApp()
//获取 log 日志打印
const log = new app.appLogs()
//获取 http 网络请求
const http = new app.appHttp()
import Dialog from '../../../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: "",
        imagewidth: 0, // 缩放后的宽
        imageheight: 0, // 缩放后的高
        activity: {},
        article_content: "",
        activityis_join: 0,
        startTime: "",
        type: "",
        endTime: "",
        placetype: '',
        signout: '',
        scancodeid: '无活动id',
        setoutmsg: '未签退',

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            theme: app.globalData.theme
        })
        const id = options.id
        const type = options.type
        this.setData({
            id: id,
            type: type
        })
        log.e(type)
        this.getactivity(id)
        this.setActivity(id)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 活动详情
     * @param event
     */
    getactivity: function (event) {
        const id = event
        const than = this
        http.get("activity/activity", {
            id: id
        }).then((res) => {
            var article_content = res.data.data.activity_desc
            article_content = article_content
                .replace(/<img/gi, '<img style="max-width:100%;height:auto;float:left;display:block" ')
                .replace(/<section/g, '<div')
                .replace(/\/section>/g, '\div>')
                .replace(/<w:LsdException (.*?)>/gi,"")
                .replace(/<\/w:LsdException>/gi,"");
            if (res.data.code == 200) {
                let start_time = res.data.data.start_time.substring(5, 16); //要截取时间的字符串
                let end_time = res.data.data.end_time.substring(5, 16); //要截取时间的字符串
                log.e()
                than.setData({
                    startTime: start_time,
                    endTime: end_time,
                    activity: res.data.data,
                    activityis_join: res.data.data.is_join,
                    article_content: article_content,
                    placetype: res.data.data.is_online,
                    signout: res.data.data.is_join_end,
                })
            }
        })
    },
    /**
     * 图片加载事件
     * @param e
     */
    imageLoad: function (e) {
        var imageSize = this.imageUtil(e)
        this.setData({
            imagewidth: imageSize.imageWidth,
            imageheight: imageSize.imageHeight
        })
    },
    /**
     * 图片处理
     * @param e
     * @returns {{}}
     */
    imageUtil: function (e) {
        var imageSize = {};
        var originalWidth = e.detail.width; // 图片原始宽
        var originalHeight = e.detail.height; // 图片原始高
        var originalScale = originalHeight / originalWidth; //图片高宽比
        //获取屏幕宽高
        wx.getSystemInfo({
            success: function (res) {
                var windowWidth = res.windowWidth;
                var windowHeight = res.windowHeight;
                var windowscale = windowHeight / windowWidth;//屏幕高宽比
                if (originalScale < windowscale) { // 图片高宽比小于屏幕高宽比
                    //图片缩放后的宽为屏幕宽
                    imageSize.imageWidth = windowWidth;
                    imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
                } else { // 图片高宽比大于屏幕高宽比
                    //图片缩放后的高为屏幕高
                    imageSize.imageHeight = windowHeight;
                    imageSize.imageWidth = (windowHeight * originalWidth) / originalHeight;
                }
            }
        })
        return imageSize;
    },
    /**
     * 上传访问量
     * @param {activityid} event
     */
    setActivity: function (event) {
        const id = event
        http.post("activity/addBrowse", {
            id: id
        }).then((res) => {
        })
    },
    /**
     * 参加活动按钮
     * @param event
     */
    onActivityClick: function (event) {
        const id = event.currentTarget.dataset.id
        const isinjon = event.currentTarget.dataset.isinjon
        const than = this
        let type = "签到"
        if (than.data.activityis_join == 0) {
            type = "签到"
        } else {
            type = "签退"
        }
        http.post("activity/join", {
            id: id,
            type: type
        }).then((res) => {
            const str = res.data.msg
            if (res.data.code == 200) {
                if (than.data.activityis_join == 0) {
                    than.setData({
                        activityis_join: 1
                    })
                } else {
                    than.setData({
                        activityis_join: 0
                    })
                }
                Dialog.alert({
                    title: '提示',
                    message: '参与成功',
                }).then(() => {
                });
            } else {
                Dialog.alert({
                    title: '提示',
                    message: str,
                }).then(() => {
                });
            }
        })
    },
    /**
     * 作品详情
     * @param event
     */
    onClickWork: function (event) {
        const id = event.currentTarget.dataset.id
        wx.navigateTo({
            url: "../work/work?id=" + id
        })
    },

    /**
     * 活动风采
     * @param e
     */
    onClickStyle: function (e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: "../style/style?id=" + id
        })
    },
    /**
     * 扫码参与活动
     */
    scancode: function () {
        const than = this
        // 允许从相机和相册扫码
        if(than.data.scancodeid==='无活动id') {
            wx.scanCode({
                success(res) {
                    than.setData({
                        scancodeid: res.result
                    })
                    if (than.data.activity.activity_id == than.data.scancodeid) {
                        http.post("activity/scan-join", {
                            id: than.data.scancodeid
                        }).then((res) => {
                            log.e(JSON.stringify(res))
                            Dialog.alert({
                                title: '提示',
                                message: res.data.msg,
                            })
                        })
                    } else {
                        than.setData({
                            scancodeid: '无活动id'
                        })
                        Dialog.alert({
                            title: '提示',
                            message: '请确认扫码活动是否为当前页面活动！',
                        })
                    }
                },
                fail: (res) => {
                    if (res.result == '') {
                        Dialog.alert({
                            title: '提示',
                            message: '请确认扫码活动是否存在或联系管理员！',
                        })
                    }
                }
            })
        }
    },
    /**
     * 线下活动签退
     * @param event
     */
    setout: function (event) {
        const than=this
        const id = event.currentTarget.dataset.id
        Dialog.confirm({
            title: '提示',
            message: '是否签退?',
        }).then(() => {
            http.post("activity/scan-join", {
                id: id
            }).then((res) => {
                than.setData({
                    setoutmsg:res.data.msg
                })
                log.e(JSON.stringify(res))
                Dialog.alert({
                    title: '提示',
                    message: res.data.msg,
                })
            })
        }).catch(() => {
            // on cancel
        });
    },
})